<template>
	<view class="recharge">
		<view class="recharge-title">
			充值金额
		</view>

		<view class="recharge-money">
			<view class="recharge-money-item" v-for="(item,index) in moneyList" :key="index"
				:class="activeIndex == index?'active':''" @click="selectMoney(index)">
				<view class="" v-if="index != 3">
					<view class="recharge-item-money">
						{{ item.money }}<span>元</span>
					</view>
					<view class="recharge-item-tip">
						金额
					</view>
				</view>
				<view class="" v-else>
					<view class="recharge-item-money">
						<input type="digit" class="InputClass" v-model="item.money" />

						<span style="margin-bottom: 0;">元</span>
					</view>
					<view class="recharge-item-tip">
						金额
					</view>
				</view>
			</view>

		</view>

		<view class="recharge-title h-m-t-20">
			充值说明
		</view>
		<view class="recharge-tip">
			<view>1.会员余额可以在店中直接付款使用。</view>
			<view>2.充值所得金额仅该小程序可用。</view>
			<view>3.最终解释权归智慧小店所属。</view>
		</view>

		<view class="recharge-buttom-box">
			<view class="buttom-left">
				<view class="buttom-left-info">付款金额</view>
				<view class="buttom-left-money">¥ {{ moneyList[activeIndex].money }}</view>
			</view>

			<view class="buttom-right" @click="okEvent">
				立即充值
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				moneyList: [{
					'id': 0,
					'money': 30
				}, {
					'id': 1,
					'money': 50
				}, {
					'id': 2,
					'money': 100
				}, {
					'id': 3,
					'money': 0
				}],
				activeIndex: 0,
				user: {}
			};

		},
		onLoad() {
			this.user = JSON.parse(wx.getStorageSync("user"))
		},
		methods: {
			/* 选择充值金额 */
			selectMoney(index) {
				this.activeIndex = index
			},
			async okEvent() {
				let _this = this
				let money = _this.moneyList[_this.activeIndex].money
				this.rechargeCombination(money)
			},
			rechargeCombination(money) {
				let a = {
					"UserId": this.user.id,
					"money": money
				}
				uni.showLoading({
					title: '加载中'
				});
				this.$request(this.$api.UserRecharge, a, "POST", false).then(res => {
					uni.hideLoading();
					let response = res.data.data
					if (res.data.status == '200') {
						wx.requestPayment({
							"timeStamp": response.timeStamp,
							"nonceStr": response.nonceStr,
							"package": response.package,
							"signType": response.signType,
							"paySign": response.paySign,
							"success": (res) => {
								uni.showToast({
									title: "支付成功！",
									icon: 'success'
								});
								setTimeout(() => {
									let pages = getCurrentPages(); // 当前页面
									let beforePage = pages[pages.length - 2]; // 上一页
									uni.navigateBack({
										success: function() {
											beforePage.onLoad(); // 执行上一页的onLoad方法
										}
									});
								}, 1000);
							},
							"fail": (res) => {
								uni.showToast({
									title: "支付失败，请重新尝试",
									icon: 'error'
								});
							},
							"complete": function(res) {}
						})
					} else {
						uni.showToast({
							title: "支付失败，请重新尝试",
							icon: 'error'
						});
					}

				})
			},

		}
	}
</script>

<style lang="scss">
	.recharge {
		margin: 30rpx;

		.recharge-title {
			color: #333333;
			font-weight: bold;
		}

		.recharge-tip {
			margin-top: 20rpx;

			view {
				font-size: 28rpx;
				color: #666666;
				margin-top: 10rpx;
			}
		}

		.recharge-money {
			display: flex;
			align-items: center;
			// overflow: hidden;
			// overflow-x: scroll;
			margin-top: 10rpx;
			flex-wrap: wrap;
			justify-content: space-around;

			.recharge-money-item:first-child {
				margin-left: 0;
			}

			.recharge-money-item {
				margin: 20rpx 10rpx;
				width: 280rpx;
				height: 360rpx;
				border: 2rpx solid #efefef;
				border-radius: 30rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				box-sizing: border-box;
				box-shadow: #f0f0f0 0px 1px 8px 0;
				flex-shrink: 0;

				.recharge-item-money {
					font-size: 60rpx;
					color: #333;
					display: flex;
					align-items: flex-end;
					justify-content: center;
					width: 100rpx;

					.InputClass {
						min-width: 86rpx;
						border-bottom: 1rpx solid;
						padding-bottom: 2rpx;
						height: 70rpx;

					}

					span {
						font-size: 28rpx;
						margin-bottom: 10rpx;
					}
				}

				.recharge-item-tip {
					padding: 6rpx 20rpx;
					font-size: 24rpx;
					background: #efefef50;
					border-radius: 40rpx;
					margin-top: 20rpx;
					color: #666666;
				}
			}

			.active {
				background-image: url("");
				background-size: 100% 100%;
				background-repeat: no-repeat;
				border: none;
			}
		}

		.recharge-buttom-box {
			position: fixed;
			bottom: 0;
			width: calc(100vw - 60rpx);
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-bottom: 80rpx;
			padding-top: 20rpx;
			border-top: 2rpx dashed #efefef;
			box-sizing: border-box;

			.buttom-left {
				font-size: 28rpx;
				display: flex;
				align-items: flex-end;

				.buttom-left-money {
					font-weight: bold;
					font-size: 40rpx;
					margin-left: 20rpx;
					line-height: 38rpx;
				}

				.buttom-left-money::first-letter {
					font-weight: normal;
					font-size: 28rpx;
				}
			}

			.buttom-right {
				padding: 16rpx 58rpx;
				background: #333;
				color: #fff;
				border-radius: 50rpx;
				font-size: 28rpx;
				font-weight: bold;
			}
		}
	}
</style>